Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add turbo.define option #71802

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from
Open

Add turbo.define option #71802

wants to merge 2 commits into from

Conversation

lforst
Copy link
Contributor

@lforst lforst commented Oct 24, 2024

Adds a turbo.define option to next.config.js to statically replace any variables during build-time for Turbopack. This is similar to Webpack's DefinePlugin.

Relates to: #71476

@ijjk ijjk added Documentation Related to Next.js' official documentation. tests type: next labels Oct 24, 2024
@ijjk
Copy link
Member

ijjk commented Oct 24, 2024

Allow CI Workflow Run

  • approve CI run for commit: 0dd20fd

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@@ -169,6 +169,7 @@ export function getDefineEnv({
}),
'process.turbopack': isTurbopack,
'process.env.TURBOPACK': isTurbopack,
...(isTurbopack ? config.experimental.turbo?.define : {}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be handled slightly differently to make sure it doesn't add keys that conflict with built-in features. Something like this to error:

const userDefines = isTurbopack ? config.experimental.turbo?.define : {}
for (const key in userDefines) {
	if(defineEnv.hasOwnProperty(key)) {
		throw new Error('descriptive error message about the defined key trying to override a built-in value')
	}
        
   	defineEnv[key] = userDefines[key]
}

@lforst
Copy link
Contributor Author

lforst commented Oct 24, 2024

I just had the dumb realization that the env option already exists. https://nextjs.org/docs/pages/api-reference/next-config-js/env

It doesn't do the exact same thing but very often it might just be enough 🤔 Feel free to make the call that this PR is unnecessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation. tests type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants